package com.nebula.api.modules.message.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nebula.api.modules.message.entity.SystemMessage;
import com.nebula.api.modules.message.service.ISysMessageService;
import link.ahsj.core.entitys.ApiModel;
import link.ahsj.core.entitys.CodeMsg;
import link.ahsj.core.utils.base.AppAssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Objects;


/**
 * @author Eastern unbeaten
 * @version Version 1.0
 * @date 2019/4/12 13:36
 * @email chenshiyun2011@163.com
 */
@Controller
@RequestMapping("api/message")
public class SysMessageController {

    @Autowired
    private ISysMessageService messageService;

    @GetMapping
    public ResponseEntity<ApiModel> messagePage(Page<SystemMessage> page, Integer type) {
        if (Objects.isNull(type)) {
            type = 0;
        }
        messageService.lambdaQuery().eq(type == 1 || type == 2, SystemMessage::getType, type).orderByDesc(SystemMessage::getTop).page(page);
        return ResponseEntity.ok(ApiModel.data(page));
    }


    @GetMapping("{id}")
    public ResponseEntity<ApiModel> getObj(@PathVariable("id") String id) {
        SystemMessage message = messageService.getById(id);
        AppAssertUtil.isNull(message, "找不到对应消息");
        return ResponseEntity.ok(ApiModel.data(message));
    }


    @PutMapping("updates")
    public ResponseEntity<ApiModel> update(String id) {
        AppAssertUtil.isBlank(id, "请选择消息!");

        messageService.lambdaUpdate().eq(SystemMessage::getId, id).set(SystemMessage::getIsReader, 0).update();

        return ResponseEntity.ok(ApiModel.success(CodeMsg.SUCCESS_MSG));
    }
}
